package org.example.fancystudy.interrupt;

import java.util.concurrent.TimeUnit;

/**
 * @author chaigq
 * @version 1.0
 * @date 2022/11/10 22:27
 */
public class InterruptDemo3 {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            for (int i = 0; i <= 300; i++){
                System.out.println("-----: "+i);
            }
            // 当前线程的中断状态
            System.out.println(Thread.currentThread().getName() + ":" + Thread.currentThread().isInterrupted());
        }, "thread");
        thread.start();
        try {
            TimeUnit.MICROSECONDS.sleep(2);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        // thread线程中断
        thread.interrupt();
        System.out.println(" 获取线程中断状态1:" + thread.isInterrupted());

        try {
            TimeUnit.SECONDS.sleep(2);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        // 此时thread线程已经执行完成，所以线程状态变为false
        System.out.println(" 获取线程中断状态2:" + thread.isInterrupted());
    }
}
